// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue'
import App from './App'
import router from './router'
import api from './api';
import directive from './directive';
import utils from './libs/utils';
import native from './libs/native';
import * as types from "./store/mutation-types";
import './less/index.less';
import store from './store'
import filter from '@/config/filter';
import NoDataPanel from '@/views/common/noDataPanel/noDataPanel';
Vue.component('no-data-panel',NoDataPanel);

// import 'swiper/dist/css/swiper.css'
// import $ from 'jquery';
// import VueAwesomeSwiper from 'vue-awesome-swiper'
// import VueClipboard from 'vue-clipboard2'
// import './style/base.css';
import './style/cover.css';
import './assets/font/font.css';

import VueLazyload from 'vue-lazyload';
Vue.use(VueLazyload,{
  preLoad: 1.3,
  error: './static/img/goods_error.png',
  loading: './static/img/goods_loading.png',
  attempt: 1
});

import { CascadePicker,ImagePreview } from 'cube-ui'
Vue.use(CascadePicker);
Vue.use(ImagePreview);

// Vue.use(VueAwesomeSwiper)
let type = utils.getBlatFrom();
if(type==1){
  store.commit(types.DEVICE_TYPE,1);
  store.commit(types.ISAPP,true);
}else if(type==2){
  store.commit(types.DEVICE_TYPE,0);
  store.commit(types.ISAPP,true);
}else if(type==3){
  store.commit(types.DEVICE_TYPE,1);
  store.commit(types.ISAPP,false);
}else if(type==4){
  store.commit(types.DEVICE_TYPE,0);
  store.commit(types.ISAPP,false);
}
// VueClipboard.config.autoSetContainer = true 
// Vue.use(VueClipboard)
Vue.config.productionTip = false
Vue.prototype.$ajax = api;
/* eslint-disable no-new */
router.beforeEach((to, from, next) => {
  document.title = to.meta.title ? to.meta.title : '';
  
  //用户信息
  if(to.query._appInfo){
      let appInfo = to.query._appInfo;
      let newAppInfo = JSON.parse(appInfo);
      if(newAppInfo.token){
        localStorage.setItem('token',newAppInfo.token);
      }else{
        localStorage.setItem('token','');
      }
      newAppInfo.appVersion = (newAppInfo.appVersion+'').replace(/\./g,'');
      sessionStorage.setItem('userInfo',appInfo);
      store.commit(types.UPDATE_USERINFO, newAppInfo);
  }
  try {
    //方式刷新从缓存去取值
    if((!store.state.user.userInfo || !store.state.user.userInfo.token)){
      let userInfo = sessionStorage.getItem('userInfo');
      if(userInfo){
        let newAppInfo = JSON.parse(userInfo);
        newAppInfo.appVersion = (newAppInfo.appVersion+'').replace(/\./g,'');
        store.commit(types.UPDATE_USERINFO, newAppInfo);
      }
    }
  } catch (error) {
    console.log(error)
  }

  next();
})
//兼容发布后 dist文件删除自动刷新页面
router.onError((e)=>{
  let reg = /^(Loading chunk )(\d+)( failed.)$/;
  let routerError = sessionStorage.getItem('routerError');
  if(routerError){
    routerError = JSON.parse(routerError);
  }
  let time = new Date().getTime();
  if(reg.test(e.message)&&(!routerError || (time - routerError.time)>1000*60*30)){
    let errorObj = {
      time:time,
      num:routerError&&routerError.num?(routerError.num+1):1
    }
    sessionStorage.setItem('routerError',JSON.stringify(errorObj));
    // window.location.reload();
    store.commit(types.ISAPP,false);
    setTimeout(()=>{
      store.commit(types.SHOW_MODAL,{
        content: "检测到新版本发布",
        btns: ["返回", "刷新"],
        callbacks: [()=>{
          native.closeWebPage();
        },()=>{
          try {
            window.location.reload();
          } catch (error) {
            native.closeWebPage();
          }
        }]
      })
    },300)
  }
})
//类别+操作+标签
//_czc.push(["_trackEvent","头部登录层","登录网站"]);
new Vue({
  el: '#app',
  router,
  store,
  components: { App },
  template: '<App/>'
})
